<?php
namespace App\Http\Controllers\ApiXcx;

use App\Service\ApiXcx\CardService;
use Illuminate\Http\Request;

class CardController extends BaseController
{
    //商品

    public function __construct()
    {
        $this->db = new CardService();
    }

    //分类列表
    public function getCateList(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $res = $this->db->getCateList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //卡片列表
    public function getCardList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $cateid = $request->post('cate_id', 0);
        $res = $this->db->getCardList($pages, $cateid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //商品详情
    public function getCardOne(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '卡片ID必须的');
        }
        $res = $this->db->getCardOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //我的礼品卡
    public function myCardList(Request $request){
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '该用户不存在');
        }
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $status = $request->post('status', 0);
        $res = $this->db->getMyCardList($pages, $uid, $status);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //使用详情
    public function useInfo(Request $request){
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $id = $request->post('id', '');
        if (!$id) {
            return $this->toJson(-2, '会员关联卡片ID必须的');
        }
        if (!$uid) {
            return $this->toJson(-2, '该用户不存在');
        }

        $res = $this->db->useInfo($id, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //卡片兑换
    public function exchange(Request $request){
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $code = $request->post('code', '');
        if (!$code) {
            return $this->toJson(-2, '卡片产品兑换码必须的');
        }
        if (!$uid) {
            return $this->toJson(-2, '该用户不存在');
        }
        $res = $this->db->exchange($code, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '兑换成功');
    }

    //领取详情
    public function cardGoodsInfo(Request $request){
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $id = $request->post('id', '');//用户关联卡片id
        if (!$id) {
            return $this->toJson(-2, '用户关联卡片ID必须的');
        }
        if (!$uid) {
            return $this->toJson(-2, '该用户不存在');
        }

        $res = $this->db->cardGoodsInfo($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //领取卡片
    public function receive(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $id = $request->post('id', '');//卡片关联用户id
        if (!$id) {
            return $this->toJson(-2, '用户关联卡片ID必须的');
        }
        $res = $this->db->receive($id, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '领取成功');
    }

    //发送卡片
    public function share(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $id = $request->post('id', '');//卡片关联用户id
        if (!$id) {
            return $this->toJson(-2, '用户关联卡片ID必须的');
        }
        $res = $this->db->share($id, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '转赠成功');
    }
}